const express = require('express')
const router = express.Router()
const ueserAction = require('../Controller/user')
const categoryAction = require('../Controller/categorys')
const xss = require('node-xss').clean;
const check = require('../mid') //检查中间件

//渲染主页
router.get('/', function (req, res) {
  categoryAction.all(function(cate){
    if (req.session.user) {
      user = req.session.user
      return res.render('index.html', {cate,user}) //渲染登录页面
    }
    res.redirect('/admin/login')
  })
})

//渲染注册页面
router.get('/register', function (req, res) {
  res.render('register.html')

})
//渲染登录页面
router.get('/login',check.isLogined, function (req, res) {
  let error = req.session.flash&& req.session.flash.error.toString()
  res.render('login.html',{error})
})


//获取用户信息
router.get('/getuse/:name',function(req,res){
  let name = req.params.name
  ueserAction.getOne(name,function(doc){
    if(doc){
      res.json(doc)
    }
  })
})


//用户登录
router.post('/login',check.checkCapt, function (req, res) {
  let user = xss(req.body)
  ueserAction.login(user, function (doc) {
    if (doc) {
      req.session.user = doc;
      res.redirect('/admin')
    } else {
      req.flash('error')
      req.flash('error','用户名或者密码错误')
      res.redirect('/admin/login') 
    }
  })
})

//用户退出
router.get('/logout', function (req, res) {
  req.session.destroy() //销毁session
  res.render('login.html')
})

//注册
router.post('/register', function (req, res) {
  let user = xss(req.body)
  ueserAction.regiter(user, function (doc) {
    if (doc) {
      req.session.user = doc
      res.redirect('/admin')
    } else {
      res.render('register.html', { err: '用户名已存在' })
    }
  })
})

//用户资料编辑
router.post('/edit-user/:name',check.isLogin,function(req,res){
  let name = req.params.name
  let user = xss(req.body)
  ueserAction.updata(name,user,function(doc){
    res.redirect('/admin/user-admin')
  })
})

// 渲染欢迎页面
router.get('/index_v1',check.isLogin, function (req, res) {
  res.render('index_v1.html')
})

//渲染用户管理页面
router.get('/user-admin',check.isLogin,check.isPower2, function (req, res) {
  ueserAction.all(function(doc){
    if(doc){
      res.render('user-admin.html',{doc})
    }else{
      res.send('服務器繁忙')
    }
  })
})

//渲染添加用户
router.get('/user-add',check.isLogin,check.isPower1, function (req, res) {
  res.render('user-add.html') 
})

//删除用户
router.get('/delete/:name',check.isLogin,check.isPower1,function (req, res) {
  let user = req.params.name
  ueserAction.delete(user,function(data){
    if(data){
      res.redirect('/admin/user-admin')
    }
    
  })
})

//添加用户
router.post('/user-add',check.isLogin,check.isPower1,function (req, res) {

  let user = xss(req.body)
  ueserAction.add(user,function(doc){
    if(doc){
     res.redirect('/admin/user-admin')
    }
  })
})

//权限
router.get('/noPower',function(req,res){
  let msg = req.session.flash&& req.session.flash.msg.toString()
  res.render('noAuthority.html',{msg})
})

//跳转页
router.get('/jump',function(req,res){
  let msg = req.session.flash&& req.session.flash.msg.toString()
  res.render('jump.html',{msg})
})


module.exports = router